﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Linq;
using QFlow.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace QFlow.Windows.Forms
{
    /// <summary>
    /// Splach form
    /// </summary>
    public partial class Splach : QFlow.Utils.BaseForm
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Splach"/> class.
        /// </summary>
        public Splach(QFlowDbContext context)
            : base(context)
        {
            InitializeComponent();
            this.Office2007ColorTable = DevComponents.DotNetBar.Rendering.eOffice2007ColorScheme.VistaGlass;
        }

        string _ConnectionString;

        /// <summary>
        /// Handles the Click event of the buttonX2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonX2_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(cmbServers.Text))
            {
                var doc = XDocument.Load("QFlow.Windows.exe.config");
                var connection = doc.Root.Elements("connectionStrings").Single().FirstNode;
                string str = ((XElement)connection).Attribute("connectionString").Value;
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(str);
                builder.DataSource = cmbServers.Text;
                ((XElement)connection).Attribute("connectionString").Value = builder.ConnectionString;
                doc.Save("QFlow.exe.config");

                builder.InitialCatalog = "master";
                SqlConnection conn = new SqlConnection(builder.ToString());
                _ConnectionString = conn.ConnectionString;


                string sql = System.IO.File.ReadAllText("initial_sql.txt", Encoding.Default);
                conn.Open();
                System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("Create database QFlowDb", conn);
                command.ExecuteNonQuery();

                command = new System.Data.SqlClient.SqlCommand(sql, conn);
                command.ExecuteNonQuery();
                conn.Close();

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        /// <summary>
        /// Raises the <see cref="E:Activated"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected override void OnActivated(EventArgs e)
        {

        }

        private void cmbServers_DropDown(object sender, EventArgs e)
        {
            DataTable dt = SqlDataSourceEnumerator.Instance.GetDataSources();
        }
    }
}
